import { toJS, makeAutoObservable } from 'mobx'
import type { QuseListType } from './interface'
import { getQuestionList } from './api'
import { api } from '@/servers/api'
import { getData } from '@/servers/http'
import { history } from 'umi'
import { LEVEL_FORM } from '@/const'
import { Toast } from 'antd-mobile'
class Store {
    questionList: QuseListType = [] /** 问题列表 */
    info: any = {} /** 关卡信息 */

    constructor() {
        makeAutoObservable(this)

        // this.getQuestionListData()
    }

    /** 获取愿景题目 */
    getQuestionListData = async (id: string) => {
        const { data } = await getQuestionList(id)
        this.questionList = data
    }

    /** 更新答案 */
    updateAnswer = (ans: string, idx: number) => {
        const questionList = toJS(this.questionList)
        this.questionList = [
            ...questionList.slice(0, idx),
            {
                ...questionList[idx],
                answer: ans,
            },
            ...questionList.slice(idx + 1),
        ]
    }

    /** 获取详情 */
    getInfo = (id: string) => {
        getData(`${api.userBarriersById}${id}`).then(res => {
            const { detail, id: levelId, type } = res.data || {}
            if (type === LEVEL_FORM) {
                this.info = res.data
                if (detail?.length > 0) {
                    /** 已填写过跳到 list 列表页 */
                    if (location.pathname.startsWith('/vision/conversation')) {
                        history.replace(`/vision/list/${levelId}`)
                        return
                    }
                    /** 回显数据 */
                    this.questionList = detail?.map((item: any) => ({
                        ...item,
                        editId: item.id,
                        ...item.barrierVisionQuestion,
                    }))
                }
            } else {
                Toast.show('关卡错误！')
            }
        })
    }

    /** 初始化 */
    initQuestionList = () => {}
}
const a = new Store()
export default a
